#encoding: utf-8
require 'api_helpers'

module GrapeAPI
  class API < Grape::API
    format    :json
    helpers GrapeAPI::ApiHelpers

    resource :users do
      post do
        _signture = {}
        for key,value in params
            _signture = JSON.parse key.gsub(/:(\w+)/){"\"#{$1}\""}.gsub('=>', ':');
        end
        _object_id = _signture["object_id"]
        _signture = _signture["signature"]

        H5timeUsers.postH5timeUsers(_object_id, _signture)

        responses_200!
      end
    end
    resource :time do
      get do
        _object_id = params["object_id"]
        _signature = params["signature"]
        _current_user = current_user(_object_id, _signature)
        _time = params["key"]

        if _current_user
          _h5time = H5time.getH5time(_object_id)
          responses_200!(_h5time)
        else
          error_30!("用户不存在")
        end

      end
      post do
        _object_id = params["object_id"]
        _signture = params["signature"]
        _current_user = current_user(_object_id, _signture)

        _data = params["data"]
        if _current_user
          H5time.postH5time(_object_id, _data)
          responses_201!
        else
          error_30!("用户不存在")
        end
      end

      put do

      end
      delete do
        _object_id = params["object_id"]
        _signture = params["signature"]
        _current_user = current_user(_object_id, _signture)

        _key = params["key"]
        if _current_user
          H5time.deleteh5time(_object_id, _key)
          responses_200!
        else
          error_30!("用户不存在")
        end
      end
    end


    resource :time_notice do
      get do
        _object_id = params["object_id"]
        _signture = params["signature"]
        _current_user = current_user(_object_id, _signture)
        _time = params["key"]

        if _current_user
          _h5timenotice = H5timeNotice.getH5timeNotice(_object_id)
          responses_200!(_h5timenotice)
        else
          error_30!("用户不存在")
        end

      end
      post do
        _object_id = params["object_id"]
        _signture = params["signature"]

        _notice_data = params["data"]
        Rails.logger.info("notice   data =======> #{_notice_data}")
        # _data = JSON.parse _notice_data
        # Rails.logger.info("notice JSON   data =======> #{_data}")
        _current_user = current_user(_object_id, _signture)
        _from_object_id = ""
        _data_key = ""
        _data_value = ""
        _notice_data.each do |key,value|
          _data_key = key
          _data_value = value
        end

        if _current_user
          H5timeNotice.postH5timeNotice(_object_id,_from_object_id, _data_key, _data_value)
          responses_201!
        else
          error_30!("用户不存在")
        end
      end

      put do

      end
      delete do
        _object_id = params["object_id"]
        _signture = params["signature"]
        _time = params["key"]
        _current_user = current_user(_object_id, _signture)

        if _current_user
          H5timeNotice.deleteh5timeNotice(_object_id, _time)
          responses_200!
        else
          error_30!("用户不存在")
        end
      end
    end
  end
end
